//购物车
import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { userstore } from './user.js'
import { cartapi, getcartapi,delcartapi } from '@/apis/cart.js'
export const usecatecart = defineStore('cart', () => {
    const userStore = userstore()
    const islogin = computed(() => {
        return userStore.value.token
    })
    //获取购物车列表
    const getcartlist = async()=>{
        const res = await getcartapi()
        cartlist.value = res.result
    }
    //购物车列表
    const cartlist = ref([])
    //商品添加到购物车
    const addcart = async (goods) => {
        const { skuId, count } = goods
        //判断是否登录
        if (islogin) {
            //已登录的购物车
            await cartapi({ skuId, count })
            const res = await getcartapi()
            cartlist.value = res.result
        }
        else {
            //未登录本地购物车
            //已添加商品则数量加一
            //没有添加的商品直接push
            const item = cartlist.value.find((item) => goods.skuId === item.skuId)
            if (item) {
                item.count++
            }
            else {
                cartlist.value.push(goods)
            }
        }
    }
    //删除购物车里面的商品
    const delgoods =async (skuId) => {
        if(islogin){
            //登录用户后删除购物车的数据
           await delcartapi([skuId])
           const res = await getcartapi()
           cartlist.value = res.result
        }
        else{
            //未登录用户删除购物车的数据
            const idx=cartlist.value.findIndex((item)=> skuId===item.skuId)
            cartlist.value.splice(idx, 1)
        }
    }

    //推出登录清除购物车里面的商品信息
    const clearlist =()=>{
        cartlist.value =[]
    }
    //计算购物车中所有商品的总数量
    const sumcount = computed(() => {
        let c = 0
        cartlist.value.forEach(item => {
            c = c + item.count
        })
        return c
    })
    //计算单间商品的总价
    const sumprice = computed(() => {
        let sum = 0
        cartlist.value.forEach(item => {
            sum = sum + item.count * item.price
        })
        return sum
    })
    //修改selected的值
    const select = (i) => {
        const istrue = cartlist.value[i].selected
        cartlist.value[i].selected = !istrue
    }
    //是否全选
    const isall = computed(() => {
        return cartlist.value.every((item) => item.selected);
    })
    //根据全选框的值来修改每个商品是否选中
    const allchange = (selected) => {
        cartlist.value.forEach(item => {
            item.selected = selected
        })
    }
    //计算购物车中选择的商品数量
    const allselectedcount = computed(() => {
        const newlist = cartlist.value.filter(item => item.selected)
        let s = 0
        newlist.forEach(item => {
            s = s + item.count
        })
        return s
    })
    //计算选中商品的总价前
    const allselectedsum = computed(() => {
        const newlist = cartlist.value.filter(item => item.selected)
        let s = 0
        newlist.forEach(item => {
            s = s + item.count * item.price
        })
        return s
    })
    return { cartlist,clearlist, addcart, getcartlist,delgoods, sumcount, sumprice, select, isall, allchange, allselectedcount, allselectedsum }
}, {
    persist: true
})